package com.minelx.datamicroscope.core.query.literalcondition;

import com.alibaba.fastjson.JSONObject;
import com.minelx.datamicroscope.core.condition.KeyOperandCondition;
import com.minelx.datamicroscope.core.query.RawConditionFactory;

class KeyOperandLiteralConditionParser implements LiteralConditionParser {

    private final RawConditionFactory rawConditionFactory;

    KeyOperandLiteralConditionParser(RawConditionFactory rawConditionFactory) {
        this.rawConditionFactory = rawConditionFactory;
    }

    @Override
    public boolean match(JSONObject data) {
        return data.containsKey("key") &&
                data.containsKey("operator") &&
                data.containsKey("value");
    }

    @Override
    public LiteralCondition createLiteralCondition(JSONObject data) {
        return LiteralCondition.keyOperand(
                rawConditionFactory,
                KeyOperandCondition.from(data));
    }
}
